package com.superjson.showpro.controller;

import com.alibaba.fastjson.JSONObject;
import com.beantools.annotation.handler.NotNullHandler;
import com.beantools.filters.BuilderFilters;
import com.beantools.util.ArrayUtil;
import com.superjson.showpro.controller.core.BaseContoller;
import com.superjson.showpro.entity.ArticleCatalog;
import com.superjson.showpro.service.ArticleCatalogService;
import com.superjson.showpro.shared.result.Result;
import com.superjson.showpro.shared.result.ResultCodeEnum;
import com.superjson.showpro.shared.result.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import static com.beantools.util.CollectionUtil.isEmpty;
import static com.superjson.showpro.shared.result.ResultCodeEnum.NO_ARTICLE;
import static com.superjson.showpro.shared.util.CommonUtil.ifTrueThrow;

/**
 * Created By zby on 19:59 2019/5/22
 */
@RestController("articleCatalogController")
@RequestMapping("/article/catalog")
public class ArticleCatalogController extends BaseContoller {

    @Autowired
    private ArticleCatalogService articleCatalogService;

    /**
     * Created By zby on 19:29 2019/6/12
     * 保存文章目录
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result saveAc(ArticleCatalog articleCatalog, Long parentId) {
        NotNullHandler.getInstance().validate(articleCatalog);
        articleCatalogService.saveAc(articleCatalog, parentId);
        return ResultUtil.buildSuccess();
    }

    /**
     * Created By zby on 19:54 2019/6/12
     * 封装目录，类似于这种情况
     * <pre>
     *  {
     *     parent:{
     *         id:""
     *         name:""
     *         content:""
     *         children:[
     *              child1:{
     *                  id:
     *                  name:
     *                  content:
     *              }
     *         ]
     *     }
     *  }
     * </pre>
     */
    @RequestMapping(value = "/withLevel", method = RequestMethod.GET)
    public Result ListWithLevel() {
        List<ArticleCatalog> acs = articleCatalogService.list(false).getResultData();
        ifTrueThrow(isEmpty(acs), NO_ARTICLE);
        String[] asJson = {"id", "name"};
        List<JSONObject> body = parseChannel2TreeJson(acs, null, asJson);
        return ResultUtil.buildSuccess(body);
    }

    private List<JSONObject> parseChannel2TreeJson(List<ArticleCatalog> acs, Long parentId, String[] asJson) {
        List<JSONObject> listRes = new ArrayList<>();
        String[] defaultJsonStr = {"id", "name"};
        asJson = (ArrayUtil.isEmpty(asJson)) ? asJson : defaultJsonStr;
        for (ArticleCatalog ac : acs) {
            boolean flag = (null == parentId && null == ac.getParent()) || (null != parentId && null != ac.getParent() && ac.getParent().getId().equals(parentId));
            if (flag) {
                JSONObject jsonObject = BuilderFilters.builderPropertyFilters().filerJson(asJson, ac);
                List<JSONObject> objects = parseChannel2TreeJson(acs, ac.getId(), asJson);
                if (null != objects && objects.size() > 0) {
                    jsonObject.put("children", objects);
                }
                listRes.add(jsonObject);
            }
        }
        return listRes;
    }

    /**
     * Created By zby on 19:54 2019/6/12
     */
    @RequestMapping(value = "/withoutLevel", method = RequestMethod.GET)
    public Result ListWithoutLevel() {
        List<ArticleCatalog> acs = articleCatalogService.list(true).getResultData();
        ifTrueThrow(isEmpty(acs), NO_ARTICLE);
        String[] acJson = {"id", "name"};
        ArrayList parents = BuilderFilters.builderPropertyFilters().filterJsons(acJson, acs);
        return ResultUtil.buildSuccess(parents);
    }


    /**
     * Created By zby on 20:35 2019/6/13
     * 获取详情
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result get(@PathVariable Long id) {
        String[] acJson = {"id", "name", "content"};
        ArticleCatalog catalog = articleCatalogService.get(id).getResultData();
        JSONObject body = BuilderFilters.builderPropertyFilters().filerJson(acJson, catalog);
        return ResultUtil.buildSuccess(body);
    }
}
